﻿using System;
using System.Reflection;
using System.IO;
using Microsoft.Win32;
using System.Diagnostics;




namespace HmmmConsole
{
    public class Program 
    {
        /// <summary>
        /// Returns Python shell path from registry
        /// </summary>
        /// <returns></returns>
        static string getPath()
        {
            RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"Python.File\shell\open\command", false);
            if(key != null)
            {
                string value = key.GetValue("") as string;
                return value.Remove(0, 1).Replace("\" \"%1\" %*", "");
            }
            else
            {
                return "";
            }
        }

        public static void Main(string[] args)
        {
            // prevent users open it directly
            if(args.Length != 1)
            {
                Console.WriteLine("Something is wrong. Did you just open it directly?");
                Console.ReadLine();
                return;
            }

            Assembly myAssembly = Assembly.GetExecutingAssembly();
            string path = myAssembly.Location;

            string pythonPath = getPath();

            if(pythonPath == "")
            {
                Console.WriteLine("Cannot find Python 3");
                Console.ReadLine();
                return;
            }

            string AssemblerPath = Path.Combine(Path.GetDirectoryName(path) , "hmmmAssembler.py");
            if(!File.Exists(AssemblerPath)) // cannot find assembler.py
            {
                Console.WriteLine("Assembler.py is missing.");
                Console.ReadLine();
                return;
            }
            //return new Program().Run(newArg);
            ProcessStartInfo assembler = new ProcessStartInfo();
            assembler.FileName = pythonPath;
            assembler.Arguments = AssemblerPath;
            assembler.UseShellExecute = false;
            assembler.RedirectStandardOutput = true;
            assembler.RedirectStandardInput = true;
            bool useSimulator = false;
            using (Process processAssembler = Process.Start(assembler))
            {
                using (StreamReader reader = processAssembler.StandardOutput)
                {
                    processAssembler.StandardInput.WriteLine(args[0]);
                    string result = reader.ReadToEnd();
                    Console.Write(result);
                    // if it assembles correctly, prompt to continue simulating
                    if (result.ToLower().Contains("assembly successful"))
                    {
                        Console.WriteLine("Do you want to run the assembly? Y/N");
                        if(Console.ReadLine().ToLower() == "y")
                        {
                            useSimulator = true;
                        }
                    }
                }
            }

            if(useSimulator)
            {
                string simPath = Path.Combine(Path.GetDirectoryName(path), "hmmmSimulator.py");
                if(!File.Exists(simPath))
                {
                    Console.WriteLine("Simulator.py is missing.");
                    Console.ReadLine();
                    return;
                }
                ProcessStartInfo simulator = new ProcessStartInfo();
                simulator.FileName = pythonPath;
                simulator.UseShellExecute = false;
                simulator.Arguments = simPath + " -i";  // fake the environment where python shell thinks it's in console
                using (Process processSimulator = Process.Start(simulator))
                {
                    processSimulator.WaitForExit();
                }
                
            }

            Console.ReadLine();

        }

    }

}
